package com.talent.jiwen.ui.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.training.data.IdNameBean;
import com.talent.jiwen.ui.widgets.flowlayout.FlowLayout;
import com.talent.jiwen.ui.widgets.flowlayout.TagAdapter;
import com.talent.jiwen.ui.widgets.flowlayout.TagFlowLayout;
import com.talent.jiwen.util.Constant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChooseIdNameDialog extends Dialog {
    TagFlowLayout gradeLayout;
    TextView titleTv;

    List<IdNameBean> dataList = new ArrayList<>();
    TagAdapter<IdNameBean> gradeAdapter;

    private Activity mContext;
    private OnTagClickListener listener;

    public ChooseIdNameDialog(Activity context) {
        super(context, R.style.myDialogTheme);
        this.mContext = context;
    }

    public ChooseIdNameDialog(Activity context, OnTagClickListener listener) {
        super(context, R.style.stardialog);
        this.mContext = context;
        this.listener=listener;
    }

    protected ChooseIdNameDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        initData();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_choose_subject, null);
        setContentView(view);

        //设置dialog大小
        Window dialogWindow = getWindow();
//        dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager manager = ((Activity) mContext).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.9);
        params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(params);
        setCanceledOnTouchOutside(true);
        initView();
    }

//    private void initData(){
//        subjectList.clear();
//        IdNameBean allBean = new IdNameBean(0 , "全部老师");
//        subjectList.add(allBean);
//
//        IdNameBean teacherBean = new IdNameBean(1 , "在校名师");
//        subjectList.add(teacherBean);
//
//        IdNameBean studentBean = new IdNameBean(2 , "名校学霸");
//        subjectList.add(studentBean);
//    }

    private void initView() {

        gradeLayout = (TagFlowLayout) findViewById(R.id.subjectLayout);
        titleTv = (TextView)findViewById(R.id.titleTv);

        titleTv.setText("选择年级");

        gradeAdapter = new TagAdapter<IdNameBean>(dataList) {
            @Override
            public View getView(FlowLayout parent, int position, final IdNameBean idNameBean) {
                TextView tv = (TextView) mContext.getLayoutInflater().inflate(R.layout.tv_tag_custom,
                        gradeLayout, false);
                tv.setText(idNameBean.getName());

                if (idNameBean.isSelected()){
                    tv.setBackgroundResource(R.drawable.tag_common_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.white));
                }else{
                    tv.setBackgroundResource(R.drawable.tag_kuang_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.text_gray_9));
                }

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        idNameBean.setSelected(true);
                        for (IdNameBean data : dataList){
                            if (data.getId() != idNameBean.getId()){
                                data.setSelected(false);
                            }
                        }
                        gradeAdapter.notifyDataChanged();

                        listener.onClick(ChooseIdNameDialog.this,idNameBean);
                        ChooseIdNameDialog.this.dismiss();
                    }
                });

                return tv;
            }
        };

        gradeLayout.setAdapter(gradeAdapter);

    }


    public void setData(List<IdNameBean> tempList){
        dataList.clear();
        dataList.addAll(tempList);
        gradeAdapter.notifyDataChanged();

    }

    public void setTagSelect(int id){
        for(IdNameBean idNameBean : dataList){
            if (idNameBean.getId() == id){
                idNameBean.setSelected(true);
            }else{
                idNameBean.setSelected(false);
            }
        }
        gradeAdapter.notifyDataChanged();
    }


    public interface OnTagClickListener{
        void onClick(Dialog dialog, IdNameBean idNameBean);
    }
}
